package sdp

import (
	"time"
)

// ContentType is the media type for an SDP session description.
const ContentType = "application/sdp"

// Session represents an SDP session description.
type Session struct {
    Version     int         // Protocol Version ("v=")
    Origin      *Origin     // Origin ("o=")
    Name        string      // Session Name ("s=")
    Information string      // Session Information ("i=")
    URI         string      // URI ("u=")
    Email       []string    // Email Address ("e=")
    Phone       []string    // Phone Number ("p=")
    Connection  *Connection // Connection Data ("c=")
    Bandwidth   Bandwidth   // Bandwidth ("b=")
    TimeZone    []*TimeZone // TimeZone ("z=")
    Key         []*Key      // Encryption Keys ("k=")
    Attributes              // Session Attributes ("a=")
    Timing      *Timing     // Timing ("t=")
    Repeat      []*Repeat   // Repeat Times ("r=")
    Media       []*Media    // Media Descriptions ("m=")
    SSRC        string      // SSRC("y=")
    Mode        string      // Streaming mode ("sendrecv", "recvonly", "sendonly", or "inactive")
}

// String returns the encoded session description as string.
func (s *Session) String() string {
    return string(s.Bytes())
}

// Bytes returns the encoded session description as buffer.
func (s *Session) Bytes() []byte {
    return new(Encoder).session(s).Bytes()
}

// Origin represents an originator of the session.
type Origin struct {
    Username       string
    SessionID      int64
    SessionVersion int64
    Network        string
    Type           string
    Address        string
}

// Connection contains connection data.
type Connection struct {
    Network    string
    Type       string
    Address    string
    TTL        int
    AddressNum int
}

// Bandwidth contains session or media bandwidth information.
type Bandwidth map[string]int

// TimeZone represents a time zones change information for a repeated session.
type TimeZone struct {
    Time   time.Time
    Offset time.Duration
}

// Key contains a key exchange information.
// Deprecated: Not recommended, supported for compatibility with older implementations.
type Key struct {
    Method, Value string
}

// Timing specifies start and stop times for a session.
type Timing struct {
    Start time.Time
    Stop  time.Time
}

// Repeat specifies repeat times for a session.
type Repeat struct {
    Interval time.Duration
    Duration time.Duration
    Offsets  []time.Duration
}

// Media contains media description.
type Media struct {
    Type    string
    Port    int
    PortNum int
    Proto   string

    Information string        // Media Information ("i=")
    Connection  []*Connection // Connection Data ("c=")
    Bandwidth   Bandwidth     // Bandwidth ("b=")
    Key         []*Key        // Encryption Keys ("k=")
    Attributes                // Attributes ("a=")

    Mode        string    // Streaming mode ("sendrecv", "recvonly", "sendonly", or "inactive")
    Formats     []*Format // Media Formats ("rtpmap")
    SSRC        string    // SSRC ("y=")
    Description string    // Description ("f=")
}

// Format returns format description by payload type.
func (m *Media) Format(pt int) *Format {
    for _, f := range m.Formats {
        if f.Payload == pt {
            return f
        }
    }
    return nil
}

// Format is a media format description represented by "rtpmap" attributes.
type Format struct {
    Payload      int
    Name         string
    ClockRate    int
    Channels     int
    Feedback     []string // "rtcp-fb" attributes
    Params       []string // "fmtp" attributes
    StreamNumber int
}

var EPOCH = time.Date(1900, time.January, 1, 0, 0, 0, 0, time.UTC)
